﻿Imports System.Data.SqlClient

Public Class frmJobOffer

    Private Sub frmJobOffer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillPositionsList()
    End Sub

    Sub FillPositionsList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From Position", cnn)
            Dim Reader As SqlDataReader

            Me.CombJobTitle.Items.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.CombJobTitle.Items.Add(Reader.Item("Position"))
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub ButClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButClose.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        ErrProv.Clear()
        If Me.txtEmpName.Text.Trim.Length = 0 Then
            ErrProv.SetError(txtEmpName, "رجاءا ادخل اسم الموظف")
            Me.txtEmpName.Focus()
            Exit Sub
        ElseIf Me.CombBranch.SelectedIndex = -1 Then
            ErrProv.SetError(CombBranch, "رجاءا ادخل الفرع")
            Me.CombBranch.Focus()
        ElseIf Me.CombJobTitle.SelectedIndex = -1 Then
            ErrProv.SetError(CombJobTitle, "رجاءا ادخل المسمى الوظيفي")
            Me.CombJobTitle.Focus()
        ElseIf Me.combHealthInsur.SelectedIndex = -1 Then
            ErrProv.SetError(combHealthInsur, "رجاءا اختر فئة التأمين الطبي")
            Me.combHealthInsur.Focus()
        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim EmpId As Integer
                Dim cmd As New SqlCommand
                Dim Trans As SqlTransaction

                cnn.Open()
                Trans = cnn.BeginTransaction
                cmd.Connection = cnn
                cmd.Transaction = Trans

                '1) built insert statement
                cmd.CommandText = "Insert Into JobOffers (EmpName,JobTitle,Branch,BasicSal,FeulAll," & _
                                  "TutFees,MobAll,TicksAll,TransportAll,HouseAll,SupervisAll,OtherAll,HealthInsur) VALUES (@EmpName,@JobTitle,@Branch,@BasicSal,@FeulAll," & _
                                  "@TutFees,@MobAll,@TicksAll,@TransportAll,@HouseAll,@SupervisAll,@OtherAll,@HealthInsur); SELECT @@IDENTITY;"

                'Assign parameters values
                cmd.Parameters.Clear()
                cmd.Parameters.AddWithValue("@EmpName", Me.txtEmpName.Text.Trim)
                cmd.Parameters.AddWithValue("@JobTitle", Me.CombJobTitle.Text)
                cmd.Parameters.AddWithValue("@Branch", Me.CombBranch.Text)
                cmd.Parameters.AddWithValue("@BasicSal", Me.txtBS.Text.Trim)
                cmd.Parameters.AddWithValue("@FeulAll", Me.txtFuel.Text.Trim)
                cmd.Parameters.AddWithValue("@TutFees", Me.txtTuitionFess.Text.Trim)
                cmd.Parameters.AddWithValue("@MobAll", Me.txtMobileAllowance.Text.Trim)
                cmd.Parameters.AddWithValue("@TicksAll", Me.txtTickValue.Text.Trim)
                cmd.Parameters.AddWithValue("@TransportAll", Me.txtTA.Text.Trim)
                cmd.Parameters.AddWithValue("@HouseAll", Me.txtHA.Text.Trim)
                cmd.Parameters.AddWithValue("@SupervisAll", txtSupervision.Text.Trim)
                cmd.Parameters.AddWithValue("@OtherAll", txtOthers.Text.Trim)
                cmd.Parameters.AddWithValue("@HealthInsur", combHealthInsur.Text)
               
                EmpId = CInt(cmd.ExecuteScalar)
                Trans.Commit()
                cnn.Close()
                
                If MsgBox("تم حفظ عرض العمل  " & Chr(13) & " رقم العرض:  " & EmpId & vbCrLf & " هل تريد ان تطبع عرض العمل؟", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    PrintID(EmpId)
                End If
                Clear()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Public Sub PrintID(ByVal EmpId As Integer)
        Try
            Dim dap As New SqlDataAdapter("Select PatNo,EmpName,JobTitle,Branch,BasicSal,FeulAll," & _
                                  "TutFees,MobAll,TicksAll,TransportAll,HouseAll,SupervisAll,OtherAll,HealthInsur From JobOffers Where EmpNo=" & EmpId, cnn1)
            Dim das As New DataSet

            cnn1.Open()
            dap.Fill(das, "JobOffers")
            cnn1.Close()

            Dim rpt As New rptJopOfferBV
            rpt.SetDataSource(das)

            ReportViewer.CrystalReportViewer1.ReportSource = rpt
            ReportViewer.ShowDialog()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If cnn1.State = ConnectionState.Open Then
                cnn1.Close()
            End If
        End Try
    End Sub

    Sub clear()
        Me.txtEmpName.Clear()
        Me.CombBranch.SelectedIndex = -1
        Me.combHealthInsur.SelectedIndex = -1
        Me.CombJobTitle.SelectedIndex = -1

        Me.txtBS.Value = 0
        Me.txtHA.Value = 0
        Me.txtTA.Value = 0
        Me.txtFuel.Value = 0
        Me.txtSupervision.Value = 0
        Me.txtTuitionFess.Value = 0
        Me.txtMobileAllowance.Value = 0
        Me.txtOthers.Value = 0
        Me.txtTickValue.Value = 0
    End Sub


End Class